package com.xyzwps.server.paimon.ui.graphql

import com.xyzwps.server.paimon.model.entity.Article
import com.xyzwps.server.paimon.ui.view.SimpleUserView
import com.xyzwps.server.paimon.ui.viewservice.ArticleViewService
import org.springframework.graphql.data.method.annotation.BatchMapping
import org.springframework.stereotype.Controller
import reactor.core.publisher.Flux

@Controller
class Mappings(private val articleViewService: ArticleViewService) {

    @BatchMapping(typeName = "ArticleView", field = "author")
    fun author(articles: List<Article>): Flux<SimpleUserView> {
        val users = articleViewService.getAuthorsMap(articles)
        return Flux.fromIterable(articles.map { a -> users[a.userId] })
    }
}
